package com.itcncp.wechat.app.rpc.service.impl;

import com.itcncp.common.annotation.BaseService;
import com.itcncp.common.base.BaseServiceImpl;
import com.itcncp.common.model.OrderCodeType;
import com.itcncp.common.util.OrderUtils;
import com.itcncp.wechat.app.dao.mapper.UserAccountInfoMapper;
import com.itcncp.wechat.app.dao.mapper.UserBankInfoMapper;
import com.itcncp.wechat.app.dao.mapper.UserRechangeOpJnlMapper;
import com.itcncp.wechat.app.dao.model.*;
import com.itcncp.wechat.app.rpc.api.UserBankInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
* UserBankInfoService实现
* Created by itcncp on 2018/1/12.
*/
@Service
@Transactional
@BaseService
public class UserBankInfoServiceImpl extends BaseServiceImpl<UserBankInfoMapper, UserBankInfo, UserBankInfoExample>
        implements UserBankInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserBankInfoServiceImpl.class);

    @Autowired
    UserBankInfoMapper userBankInfoDao;

    @Autowired
    UserAccountInfoMapper userAccountInfoDao;

    @Autowired
    UserRechangeOpJnlMapper userRechangeOpJnlDao;
    /**
     * 绑定银行卡
     * @param userBankInfo
     * @return
     * 0：失败 1成功
     */
    @Override
    public Integer addUserBarkInfo(UserBankInfo userBankInfo){

        //判断银行卡是否已经绑定
        UserBankInfo itemUserBankInfo = userBankInfoDao.selectByPrimaryKey( userBankInfo.getUserId(),userBankInfo.getBankcardNo() );
        //银行卡已存在
        if(null != itemUserBankInfo){

            if(itemUserBankInfo.getBankcardState()!=1){

                return  userBankInfoDao.updateByPrimaryKeySelective( userBankInfo );

            }

            return  0;
        }

        userBankInfo.setUpdateTime( new Date(  ) );

        userBankInfo.setCreateTime( new Date(  ) );

        //判断账户系统是否已开，目前只针对 一卡一账户
        UserAccountInfo userAccountInfo = userAccountInfoDao.selectByPrimaryKey(userBankInfo.getUserId());
        if(null == userAccountInfo){
            userAccountInfo = new UserAccountInfo();
            //添加账户系统
            userAccountInfo.setUserId( userBankInfo.getUserId() );
            //账户状态   1、正常   2、锁定
            userAccountInfo.setAccountState( new Byte( "1" ) );
            //账户余额
            userAccountInfo.setAccountBalance( new BigDecimal( "0" ) );
            //账户类型 1、个人账户 2、企业账户
            userAccountInfo.setAccountType( new Byte( "1" ) );
            //信用金额
            userAccountInfo.setCreditMoney( new BigDecimal( "0" ) );
            //服务费金额
            userAccountInfo.setServiceFee( new BigDecimal( "0" ) );
            //等级日期
            userAccountInfo.setAccountRegisteDate( new Date(  ) );

            userAccountInfoDao.insert( userAccountInfo );

            //开户记录，存入一份至充值流水表中
            UserRechangeOpJnl userRechangeOpJnl = new UserRechangeOpJnl();

            userRechangeOpJnl.setRechargeNo( OrderUtils.createOrderId( OrderCodeType.RECHARGE_NO ) );
            userRechangeOpJnl.setUserId( userBankInfo.getUserId() );
            userRechangeOpJnl.setdOrderNo( "" );
            //充值方式 1、微信 2、支付宝 3、银联 4、银行卡 5、其他
            //开户默认位ITC 其他方式
            userRechangeOpJnl.setRechargeWay( new Byte( "5" ));
            //充值金额
            userRechangeOpJnl.setRechargeAmount( new BigDecimal( "0" ) );
            //实收金额
            userRechangeOpJnl.setRechargeActAmount( new BigDecimal( "0" ) );
            //充值费率
            userRechangeOpJnl.setRechargeFee( new BigDecimal( "0" ) );
            //充值状态  1、失败； 2、成功
            userRechangeOpJnl.setRechargeState( new Byte( "2" ) );
            //充值前账户余额
            userRechangeOpJnl.setOldBalance( new BigDecimal( "0" ) );
            //充值后账户余额
            userRechangeOpJnl.setNowBalance( userAccountInfo.getAccountBalance() );

            userRechangeOpJnl.setRemark( "用户注册开户，充值记录金额默认0元" );

            userRechangeOpJnlDao.insert( userRechangeOpJnl );
        }


        return  userBankInfoDao.insertSelective( userBankInfo );
    }



}